package com.csu.letaobackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.csu.letaobackend.bean.Seller;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.persistence.SellerDao;
import com.csu.letaobackend.service.SellerService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@MapperScan("com.csu.letaobackend.persistence")
public class SellerServiceImpl implements SellerService {

      @Autowired
      private SellerDao sellerDao;

      @Override
      public CommonResponse<Seller> getSeller(String username) {
            Seller seller = sellerDao.selectById(username);

            if(seller==null){
                  return  CommonResponse.createForError("用户不存在");
            }
            return CommonResponse.createForSuccess(seller);
      }


      @Override
      public CommonResponse<Seller> getSeller(String username, String password) {
            QueryWrapper<Seller> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("username",username);
            queryWrapper.eq("password",password);

            Seller seller = sellerDao.selectOne(queryWrapper);
            if(seller==null){
                  return CommonResponse.createForError("用户名或密码错误");
            }
            return CommonResponse.createForSuccess("登录成功",seller);
      }

      @Override
      public CommonResponse<Seller> getSellerByPhone(String memPhone) {
            QueryWrapper<Seller> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("phone",memPhone);

            Seller seller = sellerDao.selectOne(queryWrapper);
            if(seller==null){
                  return CommonResponse.createForError("该手机号对应账户不存在");
            }
            return CommonResponse.createForSuccess("登录成功",seller);
      }

      @Override //测试通过
      public CommonResponse<Seller> updateSellerPassword(String username, String newPassword) {
            Seller seller = sellerDao.selectById(username);
            if(seller==null)
                  return CommonResponse.createForError("用户名不存在");
            else {
                  UpdateWrapper<Seller> updateWrapper =new UpdateWrapper<>();
                  updateWrapper.eq("username",username);
                  updateWrapper.set("password",newPassword);

                  if(sellerDao.update(null,updateWrapper)!=0)
                        return CommonResponse.createForSuccessMessage("修改成功");
                  else
                        return  CommonResponse.createForError("修改失败");
            }
      }

      @Override
      public CommonResponse<Seller> updateSeller(Seller seller) {
            Seller seller1 = sellerDao.selectById(seller.getUsername());

            if(seller1==null){
                  return  CommonResponse.createForError("用户不存在");
            }
            else{
                  sellerDao.updateById(seller);
            }
            return CommonResponse.createForSuccess("更新成功",seller);
      }

      @Override
      public CommonResponse<Seller> registerSeller(Seller seller) {
            String username = seller.getUsername();

            QueryWrapper<Seller> queryWrapper= new  QueryWrapper<>();
            queryWrapper.eq("phone",seller.getPhone());

            if (sellerDao.selectById(username)!=null){
                  return CommonResponse.createForError("用户名已存在");
            }else if(sellerDao.selectOne(queryWrapper)!=null) {
                  return CommonResponse.createForError("该手机号已注册");
            }
            else {
                  sellerDao.insert(seller);
            }
            return CommonResponse.createForSuccess("注册成功",seller);
      }


      @Override
      public CommonResponse<Seller> deleteSeller(String username) {
            Seller seller = sellerDao.selectById(username);

            if(seller==null){
                  return  CommonResponse.createForError("用户不存在");
            }
            else{
                  sellerDao.deleteById(username);
            }
            return CommonResponse.createForSuccessMessage("删除成功");
      }
}
